import { Injectable } from '@nestjs/common';
import { CreateDiscussDto } from './dto/create-discuss.dto';
import { UpdateDiscussDto } from './dto/update-discuss.dto';
import {ResultData} from "../../common/utils/result";
import {InjectRepository} from "@nestjs/typeorm";
import {Equal, Not, Repository} from "typeorm";
import {DiscussEntity} from "./entities/discuss.entity";
import {instanceToPlain} from "class-transformer";
import {QueryBlogDiscuss} from "./dto/query-blog-discuss";
import {QueryDeepPartialEntity} from "typeorm/query-builder/QueryPartialEntity";
import {CreateMessageDto} from "./dto/create-message-dto";

@Injectable()
export class DiscussService {
  constructor(
      @InjectRepository(DiscussEntity)
     private readonly DiscussEntityRepo: Repository<DiscussEntity>) {

  }
  async create(createDiscussDto: CreateDiscussDto) {
    const res = await this.DiscussEntityRepo.save(createDiscussDto);
    if(!res) return ResultData.fail(500,'评论失败',null)
    return ResultData.ok(null,'评论成功')
  }

  async createMessage(createMessageDto: CreateMessageDto) {
    const res = await this.DiscussEntityRepo.save({...createMessageDto,blogId: -1});
    if(!res) return ResultData.fail(500,'操作失败',null)
    return ResultData.ok(null,'添加留言成功')
  }

  findAll() {
    return `This action returns all discuss`;
  }

  findOne(id: number) {
    return `This action returns a #${id} discuss`;
  }

  async update(id: number, updateDiscussDto: UpdateDiscussDto) {
    const updateRes = await this.DiscussEntityRepo.update(updateDiscussDto.id,updateDiscussDto as QueryDeepPartialEntity<DiscussEntity>);
  }

  remove(id: number) {
    return `This action removes a #${id} discuss`;
  }
  async getBlogDiscuss(queryParams: QueryBlogDiscuss){
    const {size,page,blogId} = queryParams;
    const res = await this.DiscussEntityRepo.findAndCount({
      where:{
        blogId
      },
      order: { createDate: 'DESC' },
      skip: size * (page - 1) || 0,
      take: size,
    })
    if(!res) {
      return ResultData.fail(500,'评论获取失败',null)
    }
    return ResultData.ok({ list: instanceToPlain(res[0]), total: res[1] })
  }

  async getAllDiscuss(queryParams: {page:number,size: number}){
    const {size,page} = queryParams;
    const res = await this.DiscussEntityRepo.findAndCount({
      where:{
        blogId: Not(-1)
      },
      order: { createDate: 'DESC' },
      skip: size * (page - 1) || 0,
      take: size,
    })
    if(!res) {
      return ResultData.fail(500,'评论列表',null)
    }
    return ResultData.ok({ list: instanceToPlain(res[0]), total: res[1] })
  }

  async getAllMessage(queryParams: {page:number,size: number}){
    const {size,page} = queryParams;
    const res = await this.DiscussEntityRepo.findAndCount({
      where:{
        blogId: Equal(-1)
      },
      order: { createDate: 'DESC' },
      skip: size * (page - 1) || 0,
      take: size,
    })
    if(!res) {
      return ResultData.fail(500,'留言获取失败',null)
    }
    return ResultData.ok({ list: instanceToPlain(res[0]), total: res[1] })
  }
}
